
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>用户管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">用户列表</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
          <div class="layui-row">
            <div class="layui-form" style="float:right;">
              <div class="layui-form-item" style="margin:0;">
                <label class="layui-form-label">用户ID</label>
                <div class="layui-input-inline">
                  <input type="text" name="userId" id="userId" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                  <input type="text" name="userName" id="userName" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                  <select name="type" id="status" lay-search="">
                    <option value="-99">所有状态</option>
                    <option value="1">启用</option>
                    <option value="0">停止</option>
                  </select>
                </div>
                <button id="search" class="layui-btn" data-type="reload">搜索</button>
              </div>
            </div>
          </div>
          <div class="layui-row">
            <div class="layuiAdmin-btns" style="margin-bottom: 10px;">
              <a class="layui-btn layui-btn-xs" lay-href="sys/perm/user_add">新增</a>
              <button class="layui-btn layui-btn-xs" data-type="all" data-events="del">删除</button>
            </div>
          </div>
          <table id="JeePay_Mgr_User_dataAll" lay-filter="JeePay_Mgr_User_dataAll"></table>
        </div>

      </div>
    </div>
  </div>
</div>

<script type="text/html" id="jeepayBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
layui.use(['admin', 'table', 'util'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,table = layui.table
  ,element = layui.element
  ,form = layui.form;
  
  element.render('breadcrumb', 'breadcrumb');

  var tplStatus = function(d){
    if(d.status == 1) {
      return "<span style='color: green'>启用</span>";
    }
    return "<span style='color: red'>停止</span>";
  };

  //用户列表
  table.render({
    elem: '#JeePay_Mgr_User_dataAll'
    ,url: layui.setter.baseUrl + '/sys_user/list' //用户列表接口
    ,where: {
      access_token: layui.data(layui.setter.tableName).access_token
    }
    ,id: 'tableReload'
    ,page: true
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'userId', title: '用户ID'}
      ,{field: 'userName', title: '用户名'}
      ,{field: 'nickName', title: '用户昵称'}
      ,{field: 'status', title: '状态', templet: tplStatus}
      ,{field: 'createTime', title: '创建时间', templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'}
      ,{field: 'edit', title: '操作', toolbar: '#jeepayBar' }
    ]]
    ,skin: 'line'
  });

  //监听工具条
  table.on('tool(JeePay_Mgr_User_dataAll)', function(obj){
    var data = obj.data;
    if(obj.event === 'detail'){
      location.href = layui.setter.baseLocal + "sys/perm/user_view.html?" + data.userId;
    } else if(obj.event === 'del'){
      layer.confirm('确认删除么', function(index){
        admin.req(layui.setter.baseUrl + '/sys_user/delete', {userIds: data.userId}, function(){
          layer.msg('删除成功', {
            icon: 1
          });
          table.reload("tableReload");
        });
      });
    } else if(obj.event === 'edit'){
      location.href = layui.setter.baseLocal + "sys/perm/user_edit.html?" + data.userId;
    }
  });


  // 搜索
  var $ = layui.$, active = {
    reload: function(){
      var userId = $('#userId').val();
      var userName = $("#userName").val();
      var status = $("#status").val();
      //执行重载
      table.reload('tableReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
          userId: userId,
          userName: userName,
          status : status
        }
      });
    }
  };
  $('#search').on('click', function() {
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });

  // 事件处理
  var events = {
    del: function(othis, type){
      var checkStatus = table.checkStatus("tableReload")
      ,data = checkStatus.data; //获得选中的数据
      if(data.length === 0) return layer.msg('请选择数据');
      console.log(data);
      layer.confirm('确定删除选中的数据吗？', function(){
        var userIds = "";
        $.each(data, function(i, n){
          userIds = userIds + "," + n.userId;
        });
        console.log("userIds");
        if(userIds != "") {
          userIds = userIds.substring(1);
        }
        admin.req(layui.setter.baseUrl + '/sys_user/delete', {userIds: userIds}, function(){
          layer.msg('删除成功', {
            icon: 1
          });
          table.reload("tableReload"); //刷新表格
        });
      });
    }
  };
  $('.layuiAdmin-btns .layui-btn').on('click', function(){
    var othis = $(this)
            ,thisEvent = othis.data('events')
            ,type = othis.data('type');
    events[thisEvent] && events[thisEvent].call(this, othis, type);
  });
  // 渲染表单
  form.render();
});
</script>